package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.tec.teaching.lesson.school.YxyLessonsSchoolCrtItemController.RespModel;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonItemCrtController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/item/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String name = StringTrimUtil.trim(reqModel.name);
        String summary = StringTrimUtil.trim(reqModel.summary);
        String videoId = reqModel.videoId;
        Integer orderNum = 1;
        YxyLessonsCloudLessonItem item = itemRepository.findTop1ByLessonIdOrderByOrderNumDesc(lessonId);
        if (item != null) {
            orderNum = item.getOrderNum() + 1;
        }

        item = new YxyLessonsCloudLessonItem();
        item.setLessonId(lessonId);
        item.setTitle(name);
        item.setIntro(summary);
        item.setVideoId(videoId);
        // 草稿
        item.setStatus(0);
        item.setOrderNum(orderNum);
        item = itemRepository.saveAndFlush(item);

        // 修改最后跟新时间
        // 计算总课时
        YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
        if (lesson != null) {
            lesson.setUpdateDate(new Date());

            // 所有有效课时
            /**
             * Integer duration = 0;
             * 
             * List<YxyLessonsCloudLessonItem> items =
             * itemRepository.findByLessonIdAndStatusNot(lessonId, -1); for
             * (YxyLessonsCloudLessonItem it : items) { String vId =
             * it.getVideoId(); if (StringUtils.isNotBlank(vId)) {
             * YxyLessonsVideo video = videoRepository.findById(vId); if (video
             * != null) { duration = duration + video.getDuration(); } } }
             * lesson.setDuration(duration);
             */
            lessonRepository.saveAndFlush(lesson);
        }

        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String name;
        public String summary;
        public String videoId;
    }
}
